package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.User;
import com.jiawa.wiki.exception.BusinessException;
import com.jiawa.wiki.exception.BusinessExceptionCode;
import com.jiawa.wiki.mapper.UserMapper;
import com.jiawa.wiki.req.UserLoginReq;
import com.jiawa.wiki.req.UserQueryReq;
import com.jiawa.wiki.req.UserResetPasswordReq;
import com.jiawa.wiki.req.UserSaveReq;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.resp.UserLoginResp;
import com.jiawa.wiki.resp.UserQueryResp;
import com.jiawa.wiki.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public UserLoginResp login(UserLoginReq req) {
        List<User> list = userMapper.selectByLoginName(req.getLoginName());

        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        } else {
            User user1 = list.get(0);
//            System.out.println("user1: "+ req.getPassword());
            if (user1.getPassword().equals(req.getPassword())) {
                // 登录成功
                UserLoginResp user = CopyUtil.copy(user1, UserLoginResp.class);
                return user;
            } else {
                // 密码不对
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }

        }

    }
//    新增用户
    public void save(UserSaveReq req) {
        // 转换成一个类
        User user = CopyUtil.copy(req, User.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            String loginName = user.getLoginName();
            List<User> list = userMapper.addUser(loginName);
            if(list != null && list.size() > 0) {
                // 用户名已存在
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            } else {
                userMapper.insert(user);
            }
        } else {
            // 更新
            user.setLoginName(null);
            user.setPassword(null);
            userMapper.updateByPrimaryKeySelective(user);
        }
    }
  public void resetPassword(UserResetPasswordReq req) {
      User user = CopyUtil.copy(req, User.class);
      userMapper.updateByPrimaryKeySelective(user);
  }
    // 查询用户列表
    public PageResp<UserQueryResp> list(UserQueryReq req) {
        PageHelper.startPage(req.getPage(), req.getSize());
        List<User> userList = userMapper.selectByLoginName(req.getLoginName());

        PageInfo<User> pageInfo = new PageInfo<>(userList);
        // List<UserResp> respList = new ArrayList<>();
        // for (User user : userList) {
        //     // UserResp userResp = new UserResp();
        //     // BeanUtils.copyProperties(user, userResp);
        //     // 对象复制
        //     UserResp userResp = CopyUtil.copy(user, UserResp.class);
        //
        //     respList.add(userResp);
        // }

        // 列表复制
        List<UserQueryResp> list = CopyUtil.copyList(userList, UserQueryResp.class);

        PageResp<UserQueryResp> pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);

        return pageResp;
    }

    //删除用户
    public boolean delete(Long id) {
        boolean isOk = false;
        int insert = userMapper.deleteByPrimaryKey(id);
        if (insert > 0) {
            isOk = true;
        }
        return isOk;
    }
}
